import { inject } from '@angular/core';
import { Observable } from 'rxjs';

import { JsonPatchData } from '@app/core/domain/json-patch-data';
import { RequestParams } from '@app/core/domain/request-params';

import { BaseHttpService, PageItem } from './base-http.service';

export class BaseService<TEntity> {
  protected apiPrefix: string = '';
  http: BaseHttpService = inject(BaseHttpService);

  public getEntityById(id: number, params: RequestParams | TEntity = {}): Observable<TEntity[]> {
    return this.http.get<TEntity[]>(`/${this.apiPrefix}/getById/${id}`, params);
  }

  public getEntityPageList(param: RequestParams | TEntity): Observable<PageItem<TEntity>> {
    return this.http.getPage<PageItem<TEntity>>(`/${this.apiPrefix}/getall`, param);
  }

  public getEntityList(param?: RequestParams | TEntity): Observable<TEntity[]> {
    return this.http.get<TEntity[]>(`/${this.apiPrefix}/getall`, param);
  }

  public addEntity(param: TEntity[]): Observable<void> {
    return this.http.post(`/${this.apiPrefix}/add`, param, { needSuccessInfo: true });
  }

  public delEntities(ids: number[]): Observable<void> {
    return this.http.post(`/${this.apiPrefix}/delete`, ids, { needSuccessInfo: true });
  }

  public editEntity(param: TEntity[]): Observable<void> {
    return this.http.put(`/${this.apiPrefix}/put`, param, { needSuccessInfo: true });
  }

  public commonUpdate(id: number, jsonPatchData: JsonPatchData[]): Observable<TEntity> {
    return this.http.patch(`/${this.apiPrefix}/PatchById?id=${id}`, jsonPatchData, { needSuccessInfo: true, showLoading: true });
  }
}
